package cn.luke.basic.services;

import cn.luke.basic.constant.ConstantTemplate;
import cn.luke.basic.entity.DtoTemplate;
import cn.luke.basic.entity.DtoUserTemplate;
import cn.luke.basic.entity.req.ReqTemplateCount;
import cn.luke.basic.enums.EnumCountType;
import cn.luke.basic.exceptions.MyException;
import cn.luke.basic.mapper.MapperTemplate;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import cn.luke.basic.mapper.MapperUserTemplate;
import org.springframework.transaction.annotation.Transactional;


@Service
public class ServiceUserTemplate extends ServiceImpl<MapperUserTemplate, DtoUserTemplate> implements BaseService<DtoUserTemplate> {
    @Resource
    private MapperTemplate mapperTemplate;


    @Override
    public LambdaQueryWrapper<DtoUserTemplate> fillParams(Map<String, Object> params) {
        LambdaQueryWrapper<DtoUserTemplate> qw = loadQw();
        return qw;
    }


    /**
     * 查询个人关联|创建的模板
     */
    public IPage<DtoTemplate> findPageByUser(Map<String, Object> params) {
        if (ObjectUtils.isEmpty(params.get("userId")) && ObjectUtils.isEmpty(params.get("relevanceUserId"))) {
            throw new MyException("参数异常");
        }
        return mapperTemplate.findPageByUser(getPage(params),params);
    }

    /**
     * 根据模板id查询
     */
    @Transactional
    public long countByTemplateId(Integer templateId) {
        final LambdaQueryWrapper<DtoUserTemplate> wrapper = loadQw();
        wrapper.eq(DtoUserTemplate::getTemplateId, templateId);
        return count(wrapper);
    }

    /**
     * 更新相关数据量
     */
    @Transactional
    public void addCount(ReqTemplateCount reqTemplateCount) {
        final LambdaQueryWrapper<DtoUserTemplate> wr = loadQw();
        wr.eq(DtoUserTemplate::getTemplateId, reqTemplateCount.getTemplateId())
                .eq(DtoUserTemplate::getUserId, reqTemplateCount.getUserId())
        ;

        final DtoUserTemplate template = getOne(wr);
       if(template == null){
           return;
       }

        if (EnumCountType.PAGE_VIEW.getCode().equals(reqTemplateCount.getType())) {
            template.setPageView(template.getPageView()  + ConstantTemplate.COUNT);
        } else if (EnumCountType.SHARE_COUNT.getCode().equals(reqTemplateCount.getType())) {
            template.setShareCount(template.getShareCount() + ConstantTemplate.COUNT);
        } else if (EnumCountType.PARTICIPATION.getCode().equals(reqTemplateCount.getType())) {
            template.setParticipation(template.getParticipation() + ConstantTemplate.COUNT);
        }
        updateById(template);
    }

    /**
     * 根据模板和用户id查询模板详情
     */
    public DtoTemplate findById(Integer userId, Integer templateId, Integer id) {
        final LambdaQueryWrapper<DtoUserTemplate> wrapper = loadQw();
        wrapper.eq(DtoUserTemplate::getTemplateId, templateId)
                .eq(DtoUserTemplate::getId, id)
                .eq(DtoUserTemplate::getUserId, userId)
        ;
        final DtoUserTemplate userTemplate = getOne(wrapper);
        final DtoTemplate dtoTemplate = mapperTemplate.findById(templateId);

        // 获取个人创建模板的数据
        dtoTemplate.setVirtualPageView(0);
        dtoTemplate.setVirtualParticipation(0);
        dtoTemplate.setVirtualShareCount(0);
        dtoTemplate.setPageView(userTemplate.getPageView());
        dtoTemplate.setParticipation(userTemplate.getParticipation());
        dtoTemplate.setShareCount(userTemplate.getShareCount());
        return dtoTemplate;
    }

    /**
     * 根据用户id查询访问量集合
     */
    public DtoUserTemplate findByUserId(Integer userId) {
        final LambdaQueryWrapper<DtoUserTemplate> wrapper = loadQw();
        wrapper.eq(DtoUserTemplate::getUserId, userId);
        final DtoUserTemplate dtoUserTemplate = new DtoUserTemplate();
        dtoUserTemplate.setPageView(0);
        dtoUserTemplate.setParticipation(0);
        dtoUserTemplate.setShareCount(0);
        final List<DtoUserTemplate> list = list();
        for (DtoUserTemplate template : list) {
            dtoUserTemplate.setPageView(dtoUserTemplate.getPageView() + template.getPageView());
            dtoUserTemplate.setParticipation(dtoUserTemplate.getParticipation() + template.getParticipation());
            dtoUserTemplate.setShareCount(dtoUserTemplate.getShareCount() + template.getShareCount());
        }
        return dtoUserTemplate;
    }
}